from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.db import models
class UserInfoManager(BaseUserManager):
    def create_user(self, username, password=None, **extra_fields):
        if not username:
            raise ValueError('The Username field must be set')
        user = self.model(username=username, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, username, password=None, **extra_fields):
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')

        return self.create_user(username, password, **extra_fields)

class UserInfo(AbstractBaseUser, PermissionsMixin):
    """ 用户表 """
    username = models.CharField(verbose_name="用户名", max_length=63, unique=True)
    password = models.CharField(verbose_name="密码", max_length=128)
    nickname = models.CharField(verbose_name="昵称", max_length=63)
    avatar = models.ImageField(verbose_name="用户头像", upload_to='avatars/', default='avatars/defaultAvatar.png', null=True)
    loginip = models.CharField(verbose_name="登录IP", max_length=63)
    profile = models.TextField(verbose_name="个人简介", max_length=63)
    phone = models.CharField(verbose_name="手机号", max_length=63)
    email = models.CharField(verbose_name="邮箱", max_length=63)
    gender = models.CharField(verbose_name="性别", max_length=10, null=True, blank=True)
    birthday = models.DateField(verbose_name="生日", null=True, blank=True)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)

    objects = UserInfoManager()

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = []

    class Meta:
        verbose_name = '用户信息'
        verbose_name_plural = '用户信息'






class Type(models.Model):
    name = models.CharField(verbose_name="类型名称", max_length=63)
    nameid = models.CharField(verbose_name="类型标识", max_length=63, unique=True)
    jianjie = models.TextField(verbose_name="类型简介")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '类型信息'
        verbose_name_plural = '类型信息'

"""视频表"""
class Video(models.Model):
    type_nameid = models.ForeignKey(Type, on_delete=models.CASCADE, to_field='nameid', db_column='type_nameid', verbose_name="视频类型")
    ctime = models.DateTimeField(auto_now_add=True, verbose_name="发布时间")
    title = models.CharField(verbose_name="视频标题", max_length=63)
    description = models.TextField(verbose_name="视频描述")
    source = models.CharField(verbose_name="视频来源", max_length=63)
    picUrl = models.FileField(upload_to='videos_img/',verbose_name="视频图片链接", null=True,blank=True)
    url = models.FileField(upload_to='videos/', verbose_name="视频文件", null=True, blank=True)
    commitNumber = models.IntegerField(verbose_name="评论数", default=0)
    agreeNumber = models.IntegerField(verbose_name="点赞数", default=0)
    watched = models.IntegerField(verbose_name="观看数", default=0)

    def save(self, *args, **kwargs):
        if self.ctime:
            self.ctime = self.ctime.strftime("%Y-%m-%d %H:%M")
        super(Video, self).save(*args, **kwargs)

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = '视频信息'
        verbose_name_plural = '视频信息'

"""点赞表"""
class Like(models.Model):
    username = models.ForeignKey(UserInfo, on_delete=models.CASCADE, verbose_name="用户")
    like_time = models.DateTimeField(auto_now_add=True, verbose_name="点赞时间")
    item_id=models.ForeignKey(Video, on_delete=models.CASCADE, verbose_name="点赞对象ID")
    class Meta:
        verbose_name = '点赞信息'
        verbose_name_plural = '点赞信息'

"""收藏表"""
class Collect(models.Model):
    username = models.ForeignKey(UserInfo, on_delete=models.CASCADE, verbose_name="用户")
    collect_time = models.DateTimeField(auto_now_add=True, verbose_name="收藏时间")
    item_id=models.ForeignKey(Video, on_delete=models.CASCADE, verbose_name="收藏对象ID")
    class Meta:
        verbose_name = '收藏信息'
        verbose_name_plural = '收藏信息'